String.prototype.FLogToProperCase = function () {
    return this.replace(/_/g," ").replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};


function FLogProcessNav () {
    var lastSector = localStorage.getItem ("flog_last_sector");
    var sector = POgetSector ();
    var server = top.FLogGetServer ();
    
    if (sector != lastSector) {
        server.SetSector (sector);
        server.ClearData ();
        server.RefreshData();
    }
    
    FLogTrackTiles ();
    FLogDrawOverlay ();
}

function FLogTrackTiles () {
    var server = top.FLogGetServer ();
    
    for (var i=0; i<window.fieldsTotal; i++) {
        var tile = document.getElementById ("tdNavField"+i);
        if (!tile) continue;
        
        var lnk = tile.firstChild;
        if (lnk.tagName.toUpperCase() != "A" ) continue;
        
        var oncl = lnk.getAttribute ("onclick");
        var tileId;
        if (oncl) {
            tileId = oncl.split("(")[1].split(")")[0];
        } else {
            tileId = window.userloc;
        }
        
        //var save = false;
        var facility = server.GetFacility (tileId, "facility");
        if (!facility.hasOwnProperty("sector")) continue;
        var picture = FLogGetPicture (lnk);
        var btype = FLogGetBuildingType (picture);
        var tileClass = tile.getAttribute ("class").substr (3);
        if (tileClass == "Clear") {
            if (facility.hasOwnProperty("facility_type")) {
                server.RemoveFacility (tileId, facility);
                //facility["navUpdate"] = "delete_all";
                //save = true;
            }
        } else if (tileClass == "Building" || tileClass == "Planet" || tileClass == "Starbase") {
            if (facility["facility_type"] != tileClass) {
                // Update facility
                if (facility["facility_type"] != null) {
                    server.RemoveFacility (tileId, facility);
                    //facility["facility_type"] = tileClass;
                    //facility["navUpdate"] = "delete_all";
                    //save = true;
                } else {
                    // Enter a new facility
                    //facility["facility_type"] = tileClass;
                    //facility["building_type"] = btype;
                    //facility["navUpdate"] = "delete_others";
                    //save = true;
                }
            } else {
                // Update building type
                if (facility["facility_type"] == "Building" && facility["building_type"] != btype) {
                    server.RemoveFacility (tileId, facility);
                    //facility["building_type"] = btype;
                    //facility["navUpdate"] = "delete_all";
                    //save = true;
                }
            }
        }
        
        //if (save) {
        //    server.SaveFacility (tileId, facility, "facility");
        //}
    }
}

function FLogGetPicture (link) {
    var child = link.firstChild;
    var path = null;
    if (child.tagName.toUpperCase () == "IMG") {
        path = child.getAttribute ("src").split("/")
    } else if (child.tagName.toUpperCase () == "TABLE") {
        path = child.getAttribute("background");
    }
    return path[path.length-1].split(".")[0];
}

function FLogGetBuildingType (picture) {
    var ind = picture.indexOf ("_tradeoff");
    if (ind == -1) {
        return picture.FLogToProperCase ();
    } else {
        var ret = picture.substr (0, ind);
        return ret.FLogToProperCase ();
    }
}

function FLogDrawOverlay () {

    
    
}

top.FLogGetServer().AddNotification ("nav_overlay", FLogDrawOverlay);
addUserFunction (FLogProcessNav);
FLogProcessNav();